import { createTransport } from 'nodemailer'
import SMTPTransport from 'nodemailer/lib/smtp-transport'
import logger from './logger'

// 发件箱配置
const transporter = createTransport(
  new SMTPTransport({
    host: process.env.MAILER_HOST,
    port: Number(process.env.MAILER_PORT),
    secure: process.env.MAILER_SECURE === 'true',
    auth: {
      user: process.env.MAILER_USER,
      pass: process.env.MAILER_PASS,
    },
  }),
)

/**
 * 发送邮件
 * @param {String} email 收件人
 * @param {String} subject 邮件主题
 * @param {String} html html邮件内容
 */
const sendMail = async (email: string, subject: string, html: string) => {
  try {
    await transporter.sendMail({
      from: `dami_stu ${process.env.MAILER_USER}`, // 发件人
      to: email,
      subject,
      // text, // 纯文本内容
      html,
    })
  } catch (error) {
    logger.error('邮件发送失败：', error)
  }
}

export default sendMail
